﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevelopStudio.Control.Basic;
using DevelopStudio.Basic;
using System.IO;

namespace DevelopStudio.Control.FormPage
{
    public partial class SendCodeReviewForm : ChangeListFormBase
    {
        private SendCodeReviewForm()
        {
            InitializeComponent();
        }


        public SendCodeReviewForm(ChangelistInfo changelistInfo)
            : base(changelistInfo)
        {
            InitializeComponent();
            this.Text += " - " + this.ChangelistInfo.Name;
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            if (this.richTextBoxDescription.Text.Contains('[') ||
                this.richTextBoxDescription.Text.Contains(']'))
            {
                MessageBox.Show(Properties.Resources.ErrorComment);
                this.DialogResult = System.Windows.Forms.DialogResult.None;
                return;
            }

            if (File.Exists(this.textBoxPath.Text) == false)
            {
                MessageBox.Show(string.Format(Properties.Resources.ErrorInvalidChangelistPath, this.textBoxPath.Text));
                this.DialogResult = System.Windows.Forms.DialogResult.None;
                return;
            }

            this.ChangelistInfo.StoredPath = this.textBoxPath.Text;
            this.ChangelistInfo.Comment = this.richTextBoxDescription.Text;
            SearchBugs myBugs = Utility.GetSearchBugs(Utility.Settings.MyInfo.ID);
            List<BugInfo> thisBugs = myBugs.Bugs.Where(o => this.ChangelistInfo.BugInfos.Any(a => a.ID == o.ID) == true).ToList();
            string message;

            foreach (BugInfo var in this.ChangelistInfo.BugInfos)
            {
                BugInfo temp = thisBugs.Find(o => o.ID == var.ID);

                if (temp == null)
                {
                    MessageBox.Show(string.Format(Properties.Resources.ErrorNotYourBugs, var.ID, Properties.Resources.Unknown));
                    this.DialogResult = System.Windows.Forms.DialogResult.None;
                    return;
                }

                if (Utility.SupportCodeReviewBugs(temp, out message) == false ||
                    Utility.IsMyBugs(temp, Utility.Settings.MyInfo.ID, out message) == false ||
                    Utility.IsCodeReviewBug(temp, Utility.Settings.MyInfo.ID, out message) == true)
                 {
                     MessageBox.Show(message);
                     this.DialogResult = System.Windows.Forms.DialogResult.None;
                     return;
                 }
            }

            foreach (BugInfo var in thisBugs)
            {
                var.AssignedTo = new IdNamePair() { ID = (string)this.comboBoxCodeReviewer.SelectedValue, Name = this.comboBoxCodeReviewer.SelectedText };

                MemberInfo member = this.comboBoxCodeReviewer.SelectedItem as MemberInfo;

                var.AssignedTo = new IdNamePair()
                {
                    ID = member.ID,
                    Name = member.Name
                };

                if (var.UpdateRedmine(this.ChangelistInfo, AppEnum.ChangelistStatus.SendReviewing) == false)
                {
                    MessageBox.Show(string.Format(Properties.Resources.ErrorUpdateBugs, var.ID));
                    this.DialogResult = System.Windows.Forms.DialogResult.None;
                    return;
                }
            }

            this.ChangelistInfo.BugInfos.Clear();
            this.ChangelistInfo.BugInfos.AddRange(thisBugs);
            this.ChangelistInfo.Status = (int)AppEnum.ChangelistStatus.SendReviewing;
            Utility.Settings.Redmine.Members.Find(o => o.ID == (string)this.comboBoxCodeReviewer.SelectedValue).AutoCodeReviewFrequency();
            Utility.SaveEnvironmentSettings();
        }

        private void buttonPack_Click(object sender, EventArgs e)
        {
            string fileFullName = string.Empty;
            string fileName = string.Empty;
            this.ChangelistInfo.BugInfos.ForEach(o => fileName += "_" + o.ID);

            using (SaveFileDialog dlg = new SaveFileDialog())
            {
                dlg.Filter = "(*.dsdiff)|*.dsdiff";
                dlg.FileName = fileName;
                if (dlg.ShowDialog() != DialogResult.OK)
                {
                    return;
                }

                fileFullName = dlg.FileName;
            }

            try
            {
                if (Utility.PackageDiffInfo(this.ChangelistInfo, fileFullName, false) == false)
                {
                    MessageBox.Show(string.Format(Properties.Resources.ErrorPackageToFile, fileFullName));
                    return;
                }

                this.textBoxPath.Text = fileFullName;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }

        private void SendCodeReviewForm_Load(object sender, EventArgs e)
        {
            this.richTextBoxDescription.Text = this.ChangelistInfo.Comment;
            this.textBoxPath.Text = this.ChangelistInfo.StoredPath;
            this.comboBoxCodeReviewer.DataSource = Utility.Settings.Redmine.Members.OrderByDescending(o => o.CodeReviewFrequency).ThenBy(o => o.Name).ToList();
            this.comboBoxCodeReviewer.DisplayMember = "Name";
            this.comboBoxCodeReviewer.ValueMember = "ID";
        }

        private void textBoxPath_TextChanged(object sender, EventArgs e)
        {
            if (Utility.IsNetworkPath(this.textBoxPath.Text.Trim()) == true)
            {
                this.errorProviderPath.SetError(this.textBoxPath, string.Empty);
                this.buttonOk.Enabled = true;
            }
            else
            {
                this.errorProviderPath.SetError(this.textBoxPath, Properties.Resources.ErrorNetworkPath);
                this.buttonOk.Enabled = false;
            }
        }
    }
}
